//
//  ViewController.m
//  WeChat
//
//  Created by wpapple on 16/6/13.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import "WPUserInfo.h"
#import "UITextField+WP.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *enterPassword;
@property (weak, nonatomic) IBOutlet UILabel *accountLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat width = self.enterPassword.frame.size.height;
    
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Card_Lock"]];
    imageView.frame = CGRectMake(10, 0, width, width);
    
    [self.enterPassword addLeftViewWithImage:@"Card_Lock"];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    if ([defaults stringForKey:@"username"]) {
        [self.accountLabel setText:[defaults stringForKey:@"username"]];
    }else{
        [self.accountLabel setText:@"sb"];
    }
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginButton:(UIButton *)sender {
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults setObject:self.accountLabel.text forKey:@"username"];
    
    [defaults setObject:@"123456" forKey:@"password"];
    
    [defaults synchronize];
    
    [self.view endEditing:YES];
    
//    WPUserInfo * wpUserInfo = [WPUserInfo sharedWPUserInfo];
//    
//    wpUserInfo.user = @"sb";
//    wpUserInfo.pwd = @"123456";
//    
//    AppDelegate * app = [UIApplication sharedApplication].delegate;
//    
//    __weak typeof(self) w_self = self;
//    [app xmppLogin:^(XMPPState state) {
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [w_self dealXMPPSate:state];
//        });
//    }];
    
    
    NSString * userName = self.accountLabel.text;
    NSString * password = self.enterPassword.text;
    
    if (password.length > 0) {
        [self login:userName andPassword:password];
    }else{
        [self.view endEditing:YES];
        [MBProgressHUD showError:@"请输入密码" toView:self.view];
    }
    
}

//-(void)dealXMPPSate:(XMPPState)state{
//    switch (state) {
//        case XMPPLoginSuccess:
//        {
//            NSLog(@"登录成功");
//            
////            UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
////            self.view.window.rootViewController = storyboard.instantiateInitialViewController;
//            
//        }
//            
//            break;
//            
//        case XMPPLoginFailure:{
//            NSLog(@"登录失败");
//            [MBProgressHUD showError:@"账号或者密码错误" toView:self.view];
//        }
//            
//        default:
//            break;
//    }
//}

@end
